﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPipeConnectFireHydrantBatch : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				ISelectionFilter selectionFilter = new PipeAndFireHydrantSelectionFilter();
				ISelectionFilter selectionFilter2 = new PipeSelectionFilter();
				ISelectionFilter selectionFilter3 = new EquipmentSelectionFilter();
				new HYPipeConnect();
				string text = "水管连接消火栓";
				int num = 0;
				transaction = new Transaction(document, text);
				transaction.Start();
				FireHydrantTypeForm fireHydrantTypeForm = new FireHydrantTypeForm();
				DialogResult dialogResult = fireHydrantTypeForm.ShowDialog();
				string fireHdType = fireHydrantTypeForm.fireHdType;
				if (dialogResult == DialogResult.OK)
				{
					for (;;)
					{
						SubTransaction subTransaction = new SubTransaction(document);
						try
						{
							try
							{
								if (num != 0)
								{
									RevitVersionFuncs.ClearSelection(selection);
								}
								num++;
								IList<Pipe> list = new List<Pipe>();
								FamilyInstance familyInstance = null;
								new List<Element>();
								IList<Element> list2 = new List<Element>();
								IList<Element> list3 = new List<Element>();
								if (selection.GetElementIds().Any<ElementId>())
								{
									foreach (ElementId elementId in selection.GetElementIds())
									{
										list2.Add(document.GetElementById(elementId));
									}
								}
								bool flag = true;
								bool flag2 = true;
								foreach (Element element in list2)
								{
									if (element is Pipe)
									{
										list3.Add(element);
										flag = false;
									}
									else if (-2001140 == element.Category.Id.IntegerValue)
									{
										list3.Add(element);
										flag2 = false;
									}
								}
								RevitVersionFuncs.ClearSelection(selection);
								if (flag && flag2)
								{
									IEnumerator<Element> enumerator2 = selection.PickElementsByRectangle(selectionFilter, "选择水管和消火栓，其中只能包括一个设备，一根水管").GetEnumerator();
									{
										while (enumerator2.MoveNext())
										{
											Element item = enumerator2.Current;
											list3.Add(item);
										}
										goto IL_212;
									}
								}
								if (flag)
								{
									Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter2, "请选择一根水管");
									list3.Add(document.GetElement(reference));
								}
								else if (flag2)
								{
									Reference reference2 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter3, "请选择一个设备");
									list3.Add(document.GetElement(reference2));
								}
								IL_212:
								foreach (Element element2 in list3)
								{
									if (element2 is Pipe)
									{
										list.Add(element2 as Pipe);
									}
									else if (element2 is FamilyInstance)
									{
										familyInstance = (element2 as FamilyInstance);
									}
								}
								subTransaction.Start();
								if (familyInstance != null || list.Count >= 1)
								{
									if (familyInstance == null || list.Count < 1)
									{
										YJKMessageBox.Information("请选择一个消火栓和一根水管");
									}
									else
									{
										ConnFireHydrant connFireHydrant = new ConnFireHydrant(familyInstance, list);
										if (!connFireHydrant.ConnectHydrant(fireHdType))
										{
											YJKMessageBox.Information(connFireHydrant.ConnMSG);
											subTransaction.RollBack();
										}
									}
								}
								RevitVersionFuncs.ClearSelection(selection);
								document.Regenerate();
							}
							catch (Autodesk.Revit.Exceptions.OperationCanceledException)
							{
								return Autodesk.Revit.UI.Result.Succeeded;
							}
							catch (Exception ex)
							{
								YJKMessageBox.Error(ex.Message);
							}
							continue;
						}
						finally
						{
							if (subTransaction != null)
							{
								TransactionStatus status = subTransaction.GetStatus();
								if (status == (TransactionStatus)1)
								{
									subTransaction.Commit();
								}
								else if (status == (TransactionStatus)5)
								{
									subTransaction.RollBack();
								}
							}
						}
						break;
					}
				}
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				TransactionStatus status2 = transaction.GetStatus();
				if (status2 == (TransactionStatus)1)
				{
					transaction.Commit();
				}
				else if (status2 == (TransactionStatus)5)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
